#!/bin/bash
#
# The 'run' performs a simple test that verifies that S2I image.
# The main focus here is to exercise the S2I scripts.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
IMAGE_NAME=${IMAGE_NAME-centos/perl-530-centos7-candidate}

# TODO: Make command compatible for Mac users
test_dir="$(readlink -zf $(dirname "${BASH_SOURCE[0]}"))"
image_dir=$(readlink -zf ${test_dir}/..)
test_short_summary=''
TESTSUITE_RESULT=0
TEST_LIST="\
test_sample_test_app
test_binpath
test_psgi
test_psgi_variables
test_warningonstderr
test_npm
test_from_dockerfile
test_scl_variables_in_dockerfile
"

source "${test_dir}/test-lib.sh"

# TODO: This should be part of the image metadata
test_port=8080

info() {
  echo -e "\n\e[1m[INFO] $@...\e[0m\n"
}

image_exists() {
  docker inspect $1 &>/dev/null
}

container_exists() {
  image_exists $(cat $cid_file)
}

container_ip() {
  docker inspect --format="{{ .NetworkSettings.IPAddress }}" $(cat $cid_file)
}

run_s2i_build() {
  ct_s2i_build_as_df file://${test_dir}/${test_name} ${IMAGE_NAME} ${IMAGE_NAME}-testapp $(ct_build_s2i_npm_variables) "$@"
}

prepare() {
  if ! image_exists ${IMAGE_NAME}; then
    echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed."
    exit 1
  fi
  # TODO: S2I build require the application is a valid 'GIT' repository, we
  # should remove this restriction in the future when a file:// is used.
  info "Build the test application image"
  pushd ${test_dir}/${test_name} >/dev/null
  git init
  git config user.email "build@localhost" && git config user.name "builder"
  git add -A && git commit -m "Sample commit"
  popd >/dev/null
}

run_test_application() {
  docker run --user=100001 --rm --cidfile=${cid_file} ${IMAGE_NAME}-testapp
}

cleanup_test_app() {
  info "Cleaning up the test application"
  if [ -f $cid_file ]; then
    if container_exists; then
      docker stop $(cat $cid_file)
      docker rm $(cat $cid_file)
    fi
    rm $cid_file
  fi
}

cleanup() {
  info "Cleaning up the test application image"
  if image_exists ${IMAGE_NAME}-testapp; then
    docker rmi -f ${IMAGE_NAME}-testapp
  fi
  rm -rf ${test_dir}/${test_name}/.git
}

check_result() {
  local result="$1"
  if [[ "$result" != "0" ]]; then
    TESTCASE_RESULT=1
  fi
}

wait_for_cid() {
  local max_attempts=10
  local sleep_time=1
  local attempt=1
  local result=1
  info "Waiting for application container to start"
  while [ $attempt -le $max_attempts ]; do
    [ -f $cid_file ] && [ -s $cid_file ] && break
    attempt=$(( $attempt + 1 ))
    sleep $sleep_time
  done
}

test_s2i_usage() {
  info "Testing 's2i usage'"
  ct_s2i_usage ${IMAGE_NAME} ${s2i_args} &>/dev/null
}

test_docker_run_usage() {
  info "Testing 'docker run' usage"
  docker run ${IMAGE_NAME} &>/dev/null
}

test_scl_usage() {
  local run_cmd="$1"
  local expected="$2"

  info "Testing the image SCL enable"
  out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${run_cmd}")
  if ! echo "${out}" | grep -q "${expected}"; then
    echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
    return 1
  fi
  out=$(docker exec $(cat ${cid_file}) /bin/bash -c "${run_cmd}" 2>&1)
  if ! echo "${out}" | grep -q "${expected}"; then
    echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
    return 1
  fi
  out=$(docker exec $(cat ${cid_file}) /bin/sh -ic "${run_cmd}" 2>&1)
  if ! echo "${out}" | grep -q "${expected}"; then
    echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'"
    return 1
  fi
}

# Perform GET request to the application container.
# First argument is request URI path.
# Second argument is expected HTTP response code.
# Third argument is PCRE regular expression that must match the response body.
test_response() {
  local uri_path="$1"
  local expected_code="$2"
  local body_regexp="$3"

  local url="http://$(container_ip):${test_port}${uri_path}"
  info "Testing the HTTP response for <${url}>"
  local max_attempts=10
  local sleep_time=1
  local attempt=1
  local result=1
  while [ $attempt -le $max_attempts ]; do
    response=$(curl -s -w '%{http_code}' "${url}")
    status=$?
    if [ $status -eq 0 ]; then
      response_code=$(printf '%s' "$response" | tail -c 3)
      response_body=$(printf '%s' "$response" | head -c -3)
      if [ "$response_code" -eq "$expected_code" ]; then
        result=0
      fi
      printf '%s' "$response_body" | grep -qP -e "$body_regexp" || result=1;
      break
    fi
    attempt=$(( $attempt + 1 ))
    sleep $sleep_time
  done
  return $result
}

# Match PCRE regular expression against container standard output.
# First argument is the PCRE regular expression.
# It expects standard output in ${tmp_dir}/out file.
test_stdout() {
  local regexp="$1"
  local output="${tmp_dir}/out"
  info "Testing the container standard output for /${regexp}/"
  grep -qP -e "$regexp" "$output";
}

# Match PCRE regular expression against container standard error output.
# First argument is the PCRE regular expression.
# It expects error output in ${tmp_dir}/err file.
test_stderr() {
  local regexp="$1"
  local output="${tmp_dir}/err"
  info "Testing the container error output for /${regexp}/"
  grep -qP -e "$regexp" "$output";
}

test_connection() {
    test_response '/' 200 ''
}

test_application() {
  # Verify that the HTTP connection can be established to test application container
  run_test_application &

  # Wait for the container to write it's CID file
  wait_for_cid

  test_scl_usage "perl --version" "v${VERSION}."
  check_result $?

  test_connection
  check_result $?
  cleanup_test_app
}

# Build application, run it, perform test function, clean up.
# First argument is directory name.
# Second argument is function that expects running application. The function
# must return (or terminate with) non-zero value to report an failure,
# 0 otherwise.
# Other arguments are additional s2i options, like --env=FOO=bar.
# The test function have available container ID in $cid_file, container output
# in ${tmp_dir}/out, container stderr in ${tmp_dir}/err.
do_test() {
    test_name="$1"
    test_function="$2"
    shift 2

    info "Starting tests for ${test_name}."

    tmp_dir=$(mktemp -d)
    check_result $?
    cid_file="${tmp_dir}/cid"
    s2i_args="--pull-policy=never"

    # Build and run the test application
    prepare
    run_s2i_build $s2i_args "$@"
    check_result $?
    run_test_application >"${tmp_dir}/out" 2>"${tmp_dir}/err" &
    wait_for_cid

    # Perform user-supplied test function
    $test_function;
    check_result $?

    # Terminate the test application and clean up
    cleanup_test_app
    cleanup
    rm -rf "$tmp_dir"
    info "All tests for the ${test_name} finished successfully."
}


# List of tests to execute:

# This is original test that does more things like s2i API checks. Other tests
# executed by do_test() will not repeat these checks.
test_sample_test_app() {
    test_name='sample-test-app'
    info "Starting tests for ${test_name}"

    cid_file=$(mktemp -u --suffix=.cid)

    # Since we built the candidate image locally, we don't want S2I attempt to pull
    # it from Docker hub
    s2i_args="--pull-policy=never"

    prepare
    run_s2i_build $s2i_args
    check_result $?

    # Verify the 'usage' script is working properly when running the base image with 's2i usage ...'
    test_s2i_usage
    check_result $?

    # Verify the 'usage' script is working properly when running the base image with 'docker run ...'
    test_docker_run_usage
    check_result $?

    # Test application with default UID
    test_application

    # Test application with random UID
    CONTAINER_ARGS="-u 12345" test_application

    info "All tests for the ${test_name} finished successfully."
    cleanup
}

# Check scripts installed from CPAN are available to the application.
test_2_response() {
    test_response '/' 200 'Usage'
}

test_binpath() {
    do_test 'binpath' 'test_2_response'
}

# Check a single PSGI application is recognized a mod_perl is autoconfigured.
test_3_response() {
    test_response '/' 200 '<title>Web::Paste::Simple'
}

test_psgi() {
    do_test 'psgi' 'test_3_response'
}

# Check variables can select a PSGI application and set URI path.
test_4_response() {
    test_response '/path' 200 '<title>Web::Paste::Simple' && \
    test_response '/cpanfile' 200 'requires'
}

test_psgi_variables() {
    do_test 'psgi-variables' 'test_4_response' \
        '--env=PSGI_FILE=./application2.psgi' '--env=PSGI_URI_PATH=/path'
}

# Check httpd access_log flows to stdout, error_log to stdout.
# TODO: send error_log to stderr after dropping support for broken
# docker < 1.9.
test_5_response() {
    test_response '/' 200 'Text in HTTP body' && \
    test_stdout '"GET /[^"]*" 200 ' && \
    test_stdout 'Warning on stderr'
}

test_warningonstderr() {
    do_test 'warningonstderr' 'test_5_response'
}

test_npm() {
    test_name='sample-test-app'
    info "Testing npm availibility"
    # Since we built the candidate image locally, we don't want S2I attempt to pull
    # it from Docker hub
    s2i_args="--pull-policy=never"

    prepare
    run_s2i_build $s2i_args
    check_result $?

    ct_npm_works
    check_result $?

    cleanup
}

function test_scl_variables_in_dockerfile() {
  if [ "$OS" == "rhel7" ] || [ "$OS" == "centos7" ]; then
    # autocleanup only enabled here as only the following tests so far use it
    CID_FILE_DIR=$(mktemp -d)
    ct_enable_cleanup

    info "Testing variable presence during \`docker exec\`"
    ct_check_exec_env_vars
    check_result $?

    info "Checking if all scl variables are defined in Dockerfile"
    ct_check_scl_enable_vars
    check_result $?
  fi
}

function run_all_tests() {
  for test_case in $TEST_LIST; do
    : "Running test $test_case ...."
    TESTCASE_RESULT=0
    $test_case
    check_result $?
    local test_msg
    if [ $TESTCASE_RESULT -eq 0 ]; then
      test_msg="[PASSED]"
    else
      test_msg="[FAILED]"
      TESTSUITE_RESULT=1
    fi
    printf -v test_short_summary "%s %s %s\n" "${test_short_summary}" "${test_msg}" "$test_case"
    [ -n "${FAIL_QUICKLY:-}" ] && return 1
  done;
}

function test_from_dockerfile(){
  info "Check building using a Dockerfile"
  ct_test_app_dockerfile $test_dir/examples/from-dockerfile/${VERSION}/Dockerfile 'https://github.com/sclorg/dancer-ex.git' 'Welcome to your Dancer application on OpenShift' app-src
  t1=$?
  if [ "$t1" == "0" ]; then
    echo "test 1 from_dockerfile passed";
  fi;

  info "Check building using a Dockerfile.s2i"
  ct_test_app_dockerfile $test_dir/examples/from-dockerfile/${VERSION}/Dockerfile.s2i 'https://github.com/sclorg/dancer-ex.git' 'Welcome to your Dancer application on OpenShift' app-src
  t2=$?
  if [[ "$t2" == "0" ]];then
    echo "test 2 from_dockerfile passed";
  fi;

  check_result $t1
}

# Run the chosen tests
TEST_LIST=${TESTS:-$TEST_LIST} run_all_tests

echo "$test_short_summary"

if [ $TESTSUITE_RESULT -eq 0 ] ; then
  echo "Tests for ${IMAGE_NAME} succeeded."
else
  echo "Tests for ${IMAGE_NAME} failed."
fi


exit $TESTSUITE_RESULT
